<?php

/**
 * Pages form.
 *
 * @package    dolpheuscms
 * @subpackage form
 * @author     Harrie Essing
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class PagesForm extends BasePagesForm
{

    public function configure()
    {
        $this->setWidget('title', new sfWidgetFormInputText(array(), array('size' => 50)));
        $this->setWidget('url', new sfWidgetFormInputText(array(), array('size' => 50)));

        // change the input field of "content" to textarea
        $this->setWidget('content', new sfWidgetFormTextarea());
        
        // populate dropdown with all possible templates in the database
        $this->setWidget('template', new sfWidgetFormChoice(array('choices' => Doctrine_Core::getTable('Pages')->getPageTemplates())));
        $this->setValidator('template', new sfValidatorChoice(array('choices' => array_keys(Doctrine_Core::getTable('Pages')->getPageTemplates()))));
        
        // create a new widget to represent this category's "parent"
        $this->setWidget('parent', new sfWidgetFormDoctrineChoiceNestedSet(array(
                    'model' => 'Pages',
                    'add_empty' => true,
                )));

        // if the current category has a parent, make it the default choice
        if ($this->getObject()->getNode()->hasParent()) {
            $this->setDefault('parent', $this->getObject()->getNode()->getParent()->getId());
        }

        // only allow the user to change the name of the category, and its parent
        $this->useFields(array(
            'title',
            'content',
            'url',
            'template',
            'parent',
        ));
        // set labels of fields
        $this->widgetSchema->setLabels(array(
            'parent' => 'Parent page',
        ));
        // set a validator for parent which prevents a category being moved to one of its own descendants
        $this->setValidator('parent', new sfValidatorDoctrineChoiceNestedSet(array(
                    'required' => false,
                    'model' => 'Pages',
                    'node' => $this->getObject(),
                )));
        $this->getValidator('parent')->setMessage('node', 'A page cannot be made a descendent of itself.');
    }

    /**
     * Updates and saves the current object. Overrides the parent method
     * by treating the record as a node in the nested set and updating
     * the tree accordingly.
     *
     * @param Doctrine_Connection $con An optional connection parameter
     */
    public function doSave($con = null)
    {
        // save the record itself
        parent::doSave($con);
        // if a parent has been specified, add/move this node to be the child of that node
        if ($this->getValue('parent')) {
            $parent = Doctrine::getTable('Pages')->findOneById($this->getValue('parent'));
            if ($this->isNew()) {
                $this->getObject()->getNode()->insertAsLastChildOf($parent);
            }// else {
             //   $this->getObject()->getNode()->moveAsLastChildOf($parent);
            //}
        }
        // if no parent was selected, add/move this node to be a new root in the tree
        else {
            $pagesTree = Doctrine::getTable('Pages')->getTree();
            if ($this->isNew()) {
                $pagesTree->createRoot($this->getObject());
            } else {
                $this->getObject()->getNode()->makeRoot($this->getObject()->getId());
            }
        }
    }

}
